﻿/*----------------------------------------------------------------
           // Copyright (C) 2008 安华顺通(北京)科技有限公司
           // 版权所有。 
           //
           // 文件名： 
           // 文件功能描述：表字段类型编译功能。
           //
           // 
           // 创建标识：周玉偈20080916
           //
           // 修改标识
           // 修改描述：
           //
           // 修改标识：
           // 修改描述：
//----------------------------------------------------------------*/

#region UsingStatements

using System;
using System.CodeDom;
using System.CodeDom.Compiler;
using System.IO;
using System.Reflection;
using System.Runtime.Serialization;
using System.Text;
using System.Windows.Forms;
using Microsoft.CSharp;
using RuleCode.ObjectTree;

#endregion

//namespace RuleCode.ObjectTree.Compile

namespace RuleCode.compiler
{
    public class SysCompilerTableField
    {
        private static SysCompilerTableField sysCompilerTableField;
        private static readonly string EDTPath =Util.GetTableFieldsPath +"\\";// Util.OTPath + "\\Module\\TableFields\\";
        private static readonly string keyFile = "D:\\snk\\RuleCode.Query.snk";

        public static SysCompilerTableField Instance
        {
            get
            {
                if (sysCompilerTableField == null)
                {
                    sysCompilerTableField = new SysCompilerTableField();
                }
                return sysCompilerTableField;
            }
        }
       
        public void compileTableField(SysTreeNode node, string tableName)
        {
            #region 初始化环境

            Util.SetPropertyContent(node);
            string name = tableName + "_" + node.Text;
            string op;
            CodeCompileUnit CompileUnit = new CodeCompileUnit(); //编译单元容器
            //CompileUnit.AssemblyCustomAttributes.Add(new System.CodeDom.CodeAttributeDeclaration
            //    ("System.Reflection.AssemblyKeyFile", new System.CodeDom.CodeAttributeArgument(
            //    new System.CodeDom.CodePrimitiveExpression(keyFile)))); 
            CodeNamespace typeNameSpace = new CodeNamespace(Util.RootClassNameSpace + ".Table.TableField"); //命名空间
            CompileUnit.Namespaces.Add(typeNameSpace);
            CodeTypeDeclaration type = new CodeTypeDeclaration(name); //类定义，Name是类名称
            //设置类的继承。
            type.Attributes = MemberAttributes.Private;
            //type.IsPartial = true;
            type.IsClass = true;
            type.BaseTypes.Add("RuleCode.Common." + typeNameMapping(node.Property));


            // 将类型加入到容器的类型集合中
            typeNameSpace.Types.Add(type);

            //命名空间里面的引用，注意：引用在空间里面，单元里面；
            typeNameSpace.Imports.Add(new CodeNamespaceImport("System"));
            typeNameSpace.Imports.Add(new CodeNamespaceImport("System.Collections.Generic"));
            typeNameSpace.Imports.Add(new CodeNamespaceImport("System.Text"));
            typeNameSpace.Imports.Add(new CodeNamespaceImport("System.ComponentModel"));
            typeNameSpace.Imports.Add(new CodeNamespaceImport("System.Data"));
            typeNameSpace.Imports.Add(new CodeNamespaceImport("RuleCode.Common"));
           // typeNameSpace.Imports.Add(new CodeNamespaceImport("RuleCode.ExtendType"));
           // typeNameSpace.Imports.Add(new CodeNamespaceImport("RuleCode.Enum"));
            typeNameSpace.Imports.Add(new CodeNamespaceImport("RuleCode.ObjectTree"));


            //增加默认构造器
            // Declare a constructor that takes a string argument and calls the base class constructor with it.
            CodeConstructor baseStringConstructor = new CodeConstructor();
            baseStringConstructor.Attributes = MemberAttributes.Public;
            // Calls a base class constructor with the TestStringParameter parameter.
            baseStringConstructor.BaseConstructorArgs.Add(new CodeVariableReferenceExpression());

            StringBuilder sBuilder = new StringBuilder();
            sBuilder.AppendFormat("this.ExtendDataType=typeof("+Util.RootClassNameSpace+".ExtendType"+".{0});",
                                  ((TableFieldProperty)node.Property).ExtendedDataType);
            CodeSnippetStatement statement = new CodeSnippetStatement(sBuilder.ToString());
            baseStringConstructor.Statements.Add(statement);

            string typeComment = string.Empty;
            typeComment += "ExtendType is " + ((TableFieldProperty)node.Property).ExtendedDataType+"; ";

            if (node.Property is TableFieldEnumProperty)
            {
                sBuilder = new StringBuilder();

                string enumType = string.Empty;
                if (((TableFieldEnumProperty)node.Property).EnumType != "" && ((TableFieldEnumProperty)node.Property).EnumType!=null )
                    enumType = ((TableFieldEnumProperty)node.Property).EnumType;
                else
                    enumType = ((TableFieldEnumProperty)node.Property).ExtendedDataType;
                typeComment += " EnumType is " + enumType+"; ";

                sBuilder.AppendFormat("this.BaseEnumType =typeof(" + Util.RootClassNameSpace + ".Enum.{0});",
                                      enumType); //缺少反射
                statement = new CodeSnippetStatement(sBuilder.ToString());
                baseStringConstructor.Statements.Add(statement);
            }

            type.Comments.Add(new CodeCommentStatement("<summary>", true));
            type.Comments.Add(new CodeCommentStatement(typeComment , true));
            type.Comments.Add(new CodeCommentStatement("</summary>", true));
            // Adds the constructor to the Members collection of the DerivedType.
            type.Members.Add(baseStringConstructor);


            TableFieldProperty tableFieldProperty;
            tableFieldProperty = null;

            #endregion

            //还差一个Int64类型，扩展类型还差一个Enum

            #region C# Tstring

            if (node.Property is TableFieldStringProperty)
            {
                //增加带参数构造器
                // Declare a constructor that takes a string argument and calls the base class constructor with it.
                CodeConstructor baseStringConstructor1 = new CodeConstructor();
                baseStringConstructor1.Attributes = MemberAttributes.Public;
                // Declares a parameter of type string named "TestStringParameter".    
                baseStringConstructor1.Parameters.Add(new CodeParameterDeclarationExpression("System.String", "_" + name));
                // Calls a base class constructor with the TestStringParameter parameter.
                baseStringConstructor1.BaseConstructorArgs.Add(new CodeVariableReferenceExpression("_" + name));

                StringBuilder sBuilder1 = new StringBuilder();
                sBuilder1.AppendFormat("this.ExtendDataType=typeof("+Util.RootClassNameSpace+".ExtendType.{0});",
                                       ((TableFieldProperty)node.Property).ExtendedDataType);
                CodeSnippetStatement statement1 = new CodeSnippetStatement(sBuilder1.ToString());
                baseStringConstructor1.Statements.Add(statement1);

                // Adds the constructor to the Members collection of the DerivedType.
                type.Members.Add(baseStringConstructor1);

                //定义隐式转换
                op = @"
                public static implicit operator " + name + @" (string x) { return new " + name +
                     @"(x); }
                public static explicit operator string(" + name +
                     @" x) {  return x.Value; }
                public static explicit operator bool(" + name +
                     @" x) { return x.Value== DefaultValue.Value; }
                ";
                tableFieldProperty = ((TableFieldProperty)node.Property);

                type.Members.Add(new CodeSnippetTypeMember(op));
                //属性变量

                op =
                    @"
                    public override  TableFieldStringProperty Property
                    {
                        get
                        {
                            if (this.property == null)
                            {
                                this.property = new TableFieldStringProperty();
                            ";

                #region C# 定义属性

                //定义属性类
                Type mytype = tableFieldProperty.GetType();
                MemberInfo[] ms = mytype.FindMembers(MemberTypes.Property,
                                                     BindingFlags.Instance | BindingFlags.Public,
                                                     Type.FilterName, "*");
                foreach (MemberInfo m in ms)
                {
                    foreach (Attribute att in m.GetCustomAttributes(true))
                    {
                        if (att is DataMemberAttribute)
                        {
                            if (mytype.GetProperty(m.Name).GetValue(tableFieldProperty, new Object[0]) is int ||
                                mytype.GetProperty(m.Name).GetValue(tableFieldProperty, new Object[0]) is UInt32)
                            {
                                op += " this.property." + m.Name +
                                      " = " + (mytype.GetProperty(m.Name).GetValue(tableFieldProperty, new Object[0])) +
                                      " ; ";
                            }
                            else if (mytype.GetProperty(m.Name).GetValue(tableFieldProperty, new Object[0]) is DateTime)
                            {
                                op += " this.property." + m.Name +
                                      " = Convert.ToDateTime (\"" +
                                      (mytype.GetProperty(m.Name).GetValue(tableFieldProperty, new Object[0])) +
                                      "\") ; ";
                            }
                            else if (mytype.GetProperty(m.Name).GetValue(tableFieldProperty, new Object[0]) is Boolean)
                            {
                                op += " this.property." + m.Name +
                                      " = Convert.ToBoolean (\"" +
                                      (mytype.GetProperty(m.Name).GetValue(tableFieldProperty, new Object[0])) +
                                      "\") ; ";
                            }
                            else
                            {
                                op += " this.property." + m.Name +
                                      " = \"" +
                                      (string)mytype.GetProperty(m.Name).GetValue(tableFieldProperty, new Object[0]) +
                                      "\" ; ";
                            }
                        }
                    }
                }

                #endregion

                op +=
                    @"
                                }
                                if (this.property.Label == " +
                    "\"\"" +
                    @")
                                {
                                    ExtendDataType edt=(ExtendDataType )  this.ExtendDataType.GetConstructor(System.Type.EmptyTypes).Invoke(null);
                                    this.property.Label = edt.BaseProperty.Label;
				                    this.ZLabel=this.property.Label;
                                }
                                return this.property;
                            }
                        }
                        ";
                type.Members.Add(new CodeSnippetTypeMember(op));
            }

            #endregion

            #region C# TInt

            if (node.Property is TableFieldIntProperty)
            {
                //增加带参数构造器
                // Declare a constructor that takes a string argument and calls the base class constructor with it.
                CodeConstructor baseStringConstructor1 = new CodeConstructor();
                baseStringConstructor1.Attributes = MemberAttributes.Public;
                // Declares a parameter of type string named "TestStringParameter".    
                baseStringConstructor1.Parameters.Add(new CodeParameterDeclarationExpression("System.Int32", "_" + name));
                // Calls a base class constructor with the TestStringParameter parameter.
                baseStringConstructor1.BaseConstructorArgs.Add(new CodeVariableReferenceExpression("_" + name));

                StringBuilder sBuilder1 = new StringBuilder();
                sBuilder1.AppendFormat("this.ExtendDataType=typeof("+Util.RootClassNameSpace+".ExtendType.{0});",
                                       ((TableFieldProperty)node.Property).ExtendedDataType);
                CodeSnippetStatement statement1 = new CodeSnippetStatement(sBuilder1.ToString());
                baseStringConstructor1.Statements.Add(statement1);

                // Adds the constructor to the Members collection of the DerivedType.
                type.Members.Add(baseStringConstructor1);

                //定义隐式转换
                op = @"
                public static implicit operator " + name + @" (Int32 x) { return new " + name +
                     @"(x); }
                public static explicit operator Int32(" + name +
                     @" x) { return x.Value; }
                public static explicit operator bool(" + name +
                     @" x) { return x.Value==DefaultValue.Value; }
                ";
                tableFieldProperty = ((TableFieldProperty)node.Property);

                type.Members.Add(new CodeSnippetTypeMember(op));
                //属性变量

                op =
                    @"
                    public override  TableFieldIntProperty Property
                    {
                        get
                        {
                            if (this.property == null)
                            {
                                this.property = new TableFieldIntProperty();
                            ";

                #region C# 定义属性

                //定义属性类
                Type mytype = tableFieldProperty.GetType();
                MemberInfo[] ms = mytype.FindMembers(MemberTypes.Property,
                                                     BindingFlags.Instance | BindingFlags.Public,
                                                     Type.FilterName, "*");
                foreach (MemberInfo m in ms)
                {
                    foreach (Attribute att in m.GetCustomAttributes(true))
                    {
                        if (att is DataMemberAttribute)
                        {
                            if (mytype.GetProperty(m.Name).GetValue(tableFieldProperty, new Object[0]) is int)
                            {
                                op += " this.property." + m.Name +
                                      " = " + (mytype.GetProperty(m.Name).GetValue(tableFieldProperty, new Object[0])) +
                                      " ; ";
                            }
                            else if (mytype.GetProperty(m.Name).GetValue(tableFieldProperty, new Object[0]) is DateTime)
                            {
                                op += " this.property." + m.Name +
                                      " = Convert.ToDateTime (\"" +
                                      (mytype.GetProperty(m.Name).GetValue(tableFieldProperty, new Object[0])) +
                                      "\") ; ";
                            }
                            else if (mytype.GetProperty(m.Name).GetValue(tableFieldProperty, new Object[0]) is Boolean)
                            {
                                op += " this.property." + m.Name +
                                      " = Convert.ToBoolean (\"" +
                                      (mytype.GetProperty(m.Name).GetValue(tableFieldProperty, new Object[0])) +
                                      "\") ; ";
                            }
                            else
                            {
                                op += " this.property." + m.Name +
                                      " = \"" +
                                      (string)mytype.GetProperty(m.Name).GetValue(tableFieldProperty, new Object[0]) +
                                      "\" ; ";
                            }
                        }
                    }
                }

                #endregion

                op +=
                    @"
                                }
                                if (this.property.Label == " +
                    "\"\"" +
                    @")
                                {
                                    ExtendDataType edt=(ExtendDataType )  this.ExtendDataType.GetConstructor(System.Type.EmptyTypes).Invoke(null);
                                    this.property.Label = edt.BaseProperty.Label;
				                    this.ZLabel=this.property.Label;
                                }
                                return this.property;
                            }
                        }
                        ";
                type.Members.Add(new CodeSnippetTypeMember(op));
            }

            #endregion

            #region C# TDouble

            if (node.Property is TableFieldRealProperty)
            {
                //增加带参数构造器
                
                // Declare a constructor that takes a string argument and calls the base class constructor with it.
                CodeConstructor baseStringConstructor1 = new CodeConstructor();
                baseStringConstructor1.Attributes = MemberAttributes.Public;
                // Declares a parameter of type string named "TestStringParameter".    
                baseStringConstructor1.Parameters.Add(new CodeParameterDeclarationExpression("System.Double", "_" + name));
                // Calls a base class constructor with the TestStringParameter parameter.
                baseStringConstructor1.BaseConstructorArgs.Add(new CodeVariableReferenceExpression("_" + name));

                StringBuilder sBuilder1 = new StringBuilder();
                sBuilder1.AppendFormat("this.ExtendDataType=typeof("+Util.RootClassNameSpace+".ExtendType.{0});",
                                       ((TableFieldProperty)node.Property).ExtendedDataType);
                CodeSnippetStatement statement1 = new CodeSnippetStatement(sBuilder1.ToString());
                baseStringConstructor1.Statements.Add(statement1);


                // Adds the constructor to the Members collection of the DerivedType.
                type.Members.Add(baseStringConstructor1);

                //定义隐式转换
                op = @"
                public static implicit operator " + name + @" (Double x) { return new " + name +
                     @"(x); }
                public static explicit operator Double(" + name +
                     @" x) { return x.Value; }
                public static explicit operator bool(" + name +
                     @" x) { return x.Value==DefaultValue.Value; }
                ";
                tableFieldProperty = ((TableFieldProperty)node.Property);

                type.Members.Add(new CodeSnippetTypeMember(op));
                //属性变量

                op =
                    @"
                    public override  TableFieldRealProperty Property
                    {
                        get
                        {
                            if (this.property == null)
                            {
                                this.property = new TableFieldRealProperty();
                            ";

                #region C# 定义属性

                //定义属性类
                Type mytype = tableFieldProperty.GetType();
                MemberInfo[] ms = mytype.FindMembers(MemberTypes.Property,
                                                     BindingFlags.Instance | BindingFlags.Public,
                                                     Type.FilterName, "*");
                foreach (MemberInfo m in ms)
                {
                    foreach (Attribute att in m.GetCustomAttributes(true))
                    {
                        if (att is DataMemberAttribute)
                        {
                            if (mytype.GetProperty(m.Name).GetValue(tableFieldProperty, new Object[0]) is int)
                            {
                                op += " this.property." + m.Name +
                                      " = " + (mytype.GetProperty(m.Name).GetValue(tableFieldProperty, new Object[0])) +
                                      " ; ";
                            }
                            else if (mytype.GetProperty(m.Name).GetValue(tableFieldProperty, new Object[0]) is DateTime)
                            {
                                op += " this.property." + m.Name +
                                      " =Convert.ToDateTime ( \"" +
                                      (mytype.GetProperty(m.Name).GetValue(tableFieldProperty, new Object[0])) +
                                      "\" ); ";
                            }
                            else if (mytype.GetProperty(m.Name).GetValue(tableFieldProperty, new Object[0]) is Boolean)
                            {
                                op += " this.property." + m.Name +
                                      " = Convert.ToBoolean (\"" +
                                      (mytype.GetProperty(m.Name).GetValue(tableFieldProperty, new Object[0])) +
                                      "\") ; ";
                            }
                            else
                            {
                                op += " this.property." + m.Name +
                                      " = \"" +
                                      (string)mytype.GetProperty(m.Name).GetValue(tableFieldProperty, new Object[0]) +
                                      "\" ; ";
                            }
                        }
                    }
                }

                #endregion

                op +=
                    @"
                                }
                                if (this.property.Label == " +
                    "\"\"" +
                    @")
                                {
                                    ExtendDataType edt=(ExtendDataType )  this.ExtendDataType.GetConstructor(System.Type.EmptyTypes).Invoke(null);
                                    this.property.Label = edt.BaseProperty.Label;
				                    this.ZLabel=this.property.Label;
                                }
                                return this.property;
                            }
                        }
                        ";
                type.Members.Add(new CodeSnippetTypeMember(op));
            }

            #endregion

            #region C# Enum

            if (node.Property is TableFieldEnumProperty)
            {
                //增加带参数构造器
                // Declare a constructor that takes a string argument and calls the base class constructor with it.
                CodeConstructor baseStringConstructor1 = new CodeConstructor();
                baseStringConstructor1.Attributes = MemberAttributes.Public;
                // Declares a parameter of type string named "TestStringParameter".    
                baseStringConstructor1.Parameters.Add(new CodeParameterDeclarationExpression("System.Int32", "_" + name));
                // Calls a base class constructor with the TestStringParameter parameter.
                baseStringConstructor1.BaseConstructorArgs.Add(new CodeVariableReferenceExpression("_" + name));

                StringBuilder sBuilder1 = new StringBuilder();
                sBuilder1.AppendFormat("this.ExtendDataType=typeof("+Util.RootClassNameSpace+".ExtendType.{0});",
                                       ((TableFieldProperty)node.Property).ExtendedDataType);
                CodeSnippetStatement statement1 = new CodeSnippetStatement(sBuilder1.ToString());
                baseStringConstructor1.Statements.Add(statement1);

                sBuilder1 = new StringBuilder();
                string enumType = string.Empty;
                if (((TableFieldEnumProperty)node.Property).EnumType != "" && ((TableFieldEnumProperty)node.Property).EnumType != null)
                    enumType = ((TableFieldEnumProperty)node.Property).EnumType;
                else
                    enumType = ((TableFieldEnumProperty)node.Property).ExtendedDataType;

                sBuilder1.AppendFormat("this.BaseEnumType =typeof("+Util.RootClassNameSpace+".Enum.{0});",
                                       enumType);
                statement1 = new CodeSnippetStatement(sBuilder1.ToString());
                baseStringConstructor1.Statements.Add(statement1);


                // Adds the constructor to the Members collection of the DerivedType.
                type.Members.Add(baseStringConstructor1);

                //定义隐式转换
                op = @"
                public static implicit operator " + name + @" (Int32 x) { return new " + name +
                     @"(x); }
                public static explicit operator Int32(" + name +
                     @" x) {  return x.Value; }
                public static explicit operator bool(" + name +
                     @" x) { return x.Value==DefaultValue.Value; }
                ";
                //属性变量
                tableFieldProperty = ((TableFieldProperty)node.Property);

                type.Members.Add(new CodeSnippetTypeMember(op));

                op =
                    @"
                    public override  TableFieldEnumProperty Property
                    {
                        get
                        {
                            if (this.property == null)
                            {
                                this.property = new TableFieldEnumProperty();
                            ";

                #region C# 定义属性

                //定义属性类
                Type mytype = tableFieldProperty.GetType();
                MemberInfo[] ms = mytype.FindMembers(MemberTypes.Property,
                                                     BindingFlags.Instance | BindingFlags.Public,
                                                     Type.FilterName, "*");
                foreach (MemberInfo m in ms)
                {
                    foreach (Attribute att in m.GetCustomAttributes(true))
                    {
                        if (att is DataMemberAttribute)
                        {
                            if (mytype.GetProperty(m.Name).GetValue(tableFieldProperty, new Object[0]) is int)
                            {
                                op += " this.property." + m.Name +
                                      " = " + (mytype.GetProperty(m.Name).GetValue(tableFieldProperty, new Object[0])) +
                                      " ; ";
                            }
                            else if (mytype.GetProperty(m.Name).GetValue(tableFieldProperty, new Object[0]) is DateTime)
                            {
                                op += " this.property." + m.Name +
                                      " = Convert.ToDateTime (\"" +
                                      (mytype.GetProperty(m.Name).GetValue(tableFieldProperty, new Object[0])) +
                                      "\" ); ";
                            }
                            else if (mytype.GetProperty(m.Name).GetValue(tableFieldProperty, new Object[0]) is Boolean)
                            {
                                op += " this.property." + m.Name +
                                      " = Convert.ToBoolean (\"" +
                                      (mytype.GetProperty(m.Name).GetValue(tableFieldProperty, new Object[0])) +
                                      "\") ; ";
                            }
                            else
                            {
                                op += " this.property." + m.Name +
                                      " = \"" +
                                      (string)mytype.GetProperty(m.Name).GetValue(tableFieldProperty, new Object[0]) +
                                      "\" ; ";
                            }
                        }
                    }
                }

                #endregion

                op +=
                    @"
                                }
                                if (this.property.Label == " +
                    "\"\"" +
                    @")
                                {
                                    ExtendDataType edt=(ExtendDataType )  this.ExtendDataType.GetConstructor(System.Type.EmptyTypes).Invoke(null);
                                    this.property.Label = edt.BaseProperty.Label;
				                    this.ZLabel=this.property.Label;
                                }
                                return this.property;
                            }
                        }
                        ";
                string _enumName = ((TableFieldEnumProperty)(node.Property)).EnumType;
                //
                type.Members.Add(new CodeSnippetTypeMember(op));
                op =
                    @"
                //显示转换，隐式转换，以及Property属性 不改

                public override List<EnumatorProperty> EnumatorProperty
                {
                    get
                    {
                        if (this.BaseEnumType == null)
                            return null;
                        else
                        {
                            BaseEnum baseEnum =(BaseEnum ) Activator.CreateInstance(this.BaseEnumType);
                            return baseEnum.BaseEnumProperty.EnumatorPropertyList;
                        }
                    }            
                }
            ";
                type.Members.Add(new CodeSnippetTypeMember(op));
            }

            #endregion

            #region C# TContainer

            if (node.Property is TableFieldContainerProperty)
            {
                //增加带参数构造器
                // Declare a constructor that takes a string argument and calls the base class constructor with it.
                CodeConstructor baseStringConstructor1 = new CodeConstructor();
                baseStringConstructor1.Attributes = MemberAttributes.Public;
                // Declares a parameter of type string named "TestStringParameter".    
                baseStringConstructor1.Parameters.Add(new CodeParameterDeclarationExpression("Container", "_" + name));
                // Calls a base class constructor with the TestStringParameter parameter.
                baseStringConstructor1.BaseConstructorArgs.Add(new CodeVariableReferenceExpression("_" + name));

                StringBuilder sBuilder1 = new StringBuilder();
                sBuilder1.AppendFormat("this.ExtendDataType=typeof("+Util.RootClassNameSpace+".ExtendType.{0});",
                                       ((TableFieldProperty)node.Property).ExtendedDataType);
                CodeSnippetStatement statement1 = new CodeSnippetStatement(sBuilder1.ToString());
                baseStringConstructor1.Statements.Add(statement1);

                // Adds the constructor to the Members collection of the DerivedType.
                type.Members.Add(baseStringConstructor1);

                string constructorStringArg = "        public HRMCourseLocationPicture_Picture(byte [] x) :\r\n"+
                                           "            base(x)\r\n"+
                                           "        {\r\n"+
                                           "            this.ExtendDataType = typeof("+Util.RootClassNameSpace+".ExtendType." + ((TableFieldProperty)node.Property).ExtendedDataType + ");\r\n" +
                                           "        }\r\n";
                type.Members.Add(new CodeSnippetTypeMember(constructorStringArg));
                //定义隐式转换
                op = @"
                public static implicit operator " + name + @" (byte[] x) { return new " +
                     name + @"(x); }
                public static explicit operator byte[] (" + name +
                     @" x) { return x.Value; }
                public static explicit operator bool(" + name +
                     @" x) { return x.Value==DefaultValue.Value; }
                ";
                //属性变量
                tableFieldProperty = ((TableFieldProperty)node.Property);

                type.Members.Add(new CodeSnippetTypeMember(op));

                op =
                    @"
                     public override  TableFieldContainerProperty Property
                    {
                        get
                        {
                            if (this.property == null)
                            {
                                this.property = new TableFieldContainerProperty();
                            ";

                #region C# 定义属性

                //定义属性类
                Type mytype = tableFieldProperty.GetType();
                MemberInfo[] ms = mytype.FindMembers(MemberTypes.Property,
                                                     BindingFlags.Instance | BindingFlags.Public,
                                                     Type.FilterName, "*");
                foreach (MemberInfo m in ms)
                {
                    foreach (Attribute att in m.GetCustomAttributes(true))
                    {
                        if (att is DataMemberAttribute)
                        {
                            if (mytype.GetProperty(m.Name).GetValue(tableFieldProperty, new Object[0]) is int)
                            {
                                op += " this.property." + m.Name +
                                      " = " + (mytype.GetProperty(m.Name).GetValue(tableFieldProperty, new Object[0])) +
                                      " ; ";
                            }
                            else if (mytype.GetProperty(m.Name).GetValue(tableFieldProperty, new Object[0]) is DateTime)
                            {
                                op += " this.property." + m.Name +
                                      " = Convert.ToDateTime (\"" +
                                      (mytype.GetProperty(m.Name).GetValue(tableFieldProperty, new Object[0])) +
                                      "\" ); ";
                            }
                            else if (mytype.GetProperty(m.Name).GetValue(tableFieldProperty, new Object[0]) is Boolean)
                            {
                                op += " this.property." + m.Name +
                                      " = Convert.ToBoolean (\"" +
                                      (mytype.GetProperty(m.Name).GetValue(tableFieldProperty, new Object[0])) +
                                      "\") ; ";
                            }
                            else
                            {
                                op += " this.property." + m.Name +
                                      " = \"" +
                                      (string)mytype.GetProperty(m.Name).GetValue(tableFieldProperty, new Object[0]) +
                                      "\" ; ";
                            }
                        }
                    }
                }

                #endregion

                op +=
                    @"
                                }
                                if (this.property.Label == " +
                    "\"\"" +
                    @")
                                {
                                    ExtendDataType edt=(ExtendDataType )  this.ExtendDataType.GetConstructor(System.Type.EmptyTypes).Invoke(null);
                                    this.property.Label = edt.BaseProperty.Label;
				                    this.ZLabel=this.property.Label;
                                }
                                return this.property;
                            }
                        }
                        ";
                type.Members.Add(new CodeSnippetTypeMember(op));
            }

            #endregion

            #region C# TDate

            if (node.Property is TableFieldDateProperty)
            {
                //增加带参数构造器
                // Declare a constructor that takes a string argument and calls the base class constructor with it.
                CodeConstructor baseStringConstructor1 = new CodeConstructor();
                baseStringConstructor1.Attributes = MemberAttributes.Public;
                // Declares a parameter of type string named "TestStringParameter".    
                baseStringConstructor1.Parameters.Add(new CodeParameterDeclarationExpression("DateTime", "_" + name));
                // Calls a base class constructor with the TestStringParameter parameter.
                baseStringConstructor1.BaseConstructorArgs.Add(new CodeVariableReferenceExpression("_" + name));

                StringBuilder sBuilder1 = new StringBuilder();
                sBuilder1.AppendFormat("this.ExtendDataType=typeof("+Util.RootClassNameSpace+".ExtendType.{0});",
                                       ((TableFieldProperty)node.Property).ExtendedDataType);
                CodeSnippetStatement statement1 = new CodeSnippetStatement(sBuilder1.ToString());
                baseStringConstructor1.Statements.Add(statement1);

                // Adds the constructor to the Members collection of the DerivedType.
                type.Members.Add(baseStringConstructor1);

                //定义隐式转换
                op = @"
                public static implicit operator " + name + @" (DateTime x) { return new " +
                     name + @"(x); }
                public static explicit operator DateTime(" + name +
                     @" x) { return x.Value; }
                public static explicit operator bool(" + name +
                     @" x) { return x.Value==DefaultValue.Value; }
                ";
                tableFieldProperty = ((TableFieldProperty)node.Property);

                type.Members.Add(new CodeSnippetTypeMember(op));
                //属性变量

                op =
                    @"
                    public override  TableFieldDateProperty Property
                    {
                        get
                        {
                            if (this.property == null)
                            {
                                this.property = new TableFieldDateProperty();
                            ";

                #region C# 定义属性

                //定义属性类
                Type mytype = tableFieldProperty.GetType();
                MemberInfo[] ms = mytype.FindMembers(MemberTypes.Property,
                                                     BindingFlags.Instance | BindingFlags.Public,
                                                     Type.FilterName, "*");
                foreach (MemberInfo m in ms)
                {
                    foreach (Attribute att in m.GetCustomAttributes(true))
                    {
                        if (att is DataMemberAttribute)
                        {
                            if (mytype.GetProperty(m.Name).GetValue(tableFieldProperty, new Object[0]) is int)
                            {
                                op += " this.property." + m.Name +
                                      " = " + (mytype.GetProperty(m.Name).GetValue(tableFieldProperty, new Object[0])) +
                                      " ; ";
                            }
                            else if (mytype.GetProperty(m.Name).GetValue(tableFieldProperty, new Object[0]) is DateTime)
                            {
                                op += " this.property." + m.Name +
                                      " = Convert.ToDateTime (\"" +
                                      (mytype.GetProperty(m.Name).GetValue(tableFieldProperty, new Object[0])) +
                                      "\" ); ";
                            }
                            else if (mytype.GetProperty(m.Name).GetValue(tableFieldProperty, new Object[0]) is Boolean)
                            {
                                op += " this.property." + m.Name +
                                      " = Convert.ToBoolean (\"" +
                                      (mytype.GetProperty(m.Name).GetValue(tableFieldProperty, new Object[0])) +
                                      "\") ; ";
                            }
                            else
                            {
                                op += " this.property." + m.Name +
                                      " = \"" +
                                      (string)mytype.GetProperty(m.Name).GetValue(tableFieldProperty, new Object[0]) +
                                      "\" ; ";
                            }
                        }
                    }
                }

                #endregion

                op +=
                    @"
                                }
                                if (this.property.Label == " +
                    "\"\"" +
                    @")
                                {
                                    ExtendDataType edt=(ExtendDataType )  this.ExtendDataType.GetConstructor(System.Type.EmptyTypes).Invoke(null);
                                    this.property.Label = edt.BaseProperty.Label;
				                    this.ZLabel=this.property.Label;
                                }
                                return this.property;
                            }
                        }
                        ";
                type.Members.Add(new CodeSnippetTypeMember(op));
            }

            #endregion

            #region C# TTime

            if (node.Property is TableFieldTimeProperty)
            {
                //增加带参数构造器
                // Declare a constructor that takes a string argument and calls the base class constructor with it.
                CodeConstructor baseStringConstructor1 = new CodeConstructor();
                baseStringConstructor1.Attributes = MemberAttributes.Public;
                // Declares a parameter of type string named "TestStringParameter".    
                baseStringConstructor1.Parameters.Add(new CodeParameterDeclarationExpression("System.Int32", "_" + name));
                // Calls a base class constructor with the TestStringParameter parameter.
                baseStringConstructor1.BaseConstructorArgs.Add(new CodeVariableReferenceExpression("_" + name));

                StringBuilder sBuilder1 = new StringBuilder();
                sBuilder1.AppendFormat("this.ExtendDataType=typeof("+Util.RootClassNameSpace+".ExtendType.{0});",
                                       ((TableFieldProperty)node.Property).ExtendedDataType);
                CodeSnippetStatement statement1 = new CodeSnippetStatement(sBuilder1.ToString());
                baseStringConstructor1.Statements.Add(statement1);

                // Adds the constructor to the Members collection of the DerivedType.
                type.Members.Add(baseStringConstructor1);

                //定义隐式转换
                op = @"
                public static implicit operator " + name + @" (Int32 x) { return new " + name +
                     @"(x); }
                public static explicit operator Int32(" + name +
                     @" x) {  return x.Value; }
                public static explicit operator bool(" + name +
                     @" x) { return x.Value==DefaultValue.Value; }
                ";
                tableFieldProperty = ((TableFieldProperty)node.Property);
                type.Members.Add(new CodeSnippetTypeMember(op));
                //属性变量

                op =
                    @"
                     public override  TableFieldTimeProperty Property
                    {
                        get
                        {
                            if (this.property == null)
                            {
                                this.property = new TableFieldTimeProperty();
                            ";

                #region C# 定义属性

                //定义属性类
                Type mytype = tableFieldProperty.GetType();
                MemberInfo[] ms = mytype.FindMembers(MemberTypes.Property,
                                                     BindingFlags.Instance | BindingFlags.Public,
                                                     Type.FilterName, "*");
                foreach (MemberInfo m in ms)
                {
                    foreach (Attribute att in m.GetCustomAttributes(true))
                    {
                        if (att is DataMemberAttribute)
                        {
                            if (mytype.GetProperty(m.Name).GetValue(tableFieldProperty, new Object[0]) is int)
                            {
                                op += " this.property." + m.Name +
                                      " = " + (mytype.GetProperty(m.Name).GetValue(tableFieldProperty, new Object[0])) +
                                      " ; ";
                            }
                            else if (mytype.GetProperty(m.Name).GetValue(tableFieldProperty, new Object[0]) is DateTime)
                            {
                                op += " this.property." + m.Name +
                                      " = Convert.ToDateTime (\"" +
                                      (mytype.GetProperty(m.Name).GetValue(tableFieldProperty, new Object[0])) +
                                      "\" ); ";
                            }
                            else if (mytype.GetProperty(m.Name).GetValue(tableFieldProperty, new Object[0]) is Boolean)
                            {
                                op += " this.property." + m.Name +
                                      " = Convert.ToBoolean (\"" +
                                      (mytype.GetProperty(m.Name).GetValue(tableFieldProperty, new Object[0])) +
                                      "\") ; ";
                            }
                            else
                            {
                                op += " this.property." + m.Name +
                                      " = \"" +
                                      (string)mytype.GetProperty(m.Name).GetValue(tableFieldProperty, new Object[0]) +
                                      "\" ; ";
                            }
                        }
                    }
                }

                #endregion

                op +=
                    @"
                                }
                                if (this.property.Label == " +
                    "\"\"" +
                    @")
                                {
                                    ExtendDataType edt=(ExtendDataType )  this.ExtendDataType.GetConstructor(System.Type.EmptyTypes).Invoke(null);
                                    this.property.Label = edt.BaseProperty.Label;
				                    this.ZLabel=this.property.Label;
                                }
                                return this.property;
                            }
                        }
                        ";
                type.Members.Add(new CodeSnippetTypeMember(op));
            }

            #endregion

            //有的属性没加

            #region 输出与编译

            //编译环境变量
            CSharpCodeProvider provider = new CSharpCodeProvider();
            CodeGeneratorOptions options = new CodeGeneratorOptions();
            options.BlankLinesBetweenMembers = false;
            options.IndentString = "\t";

            //编译参数
            //相当于项目下的“引用”部分
            CompilerParameters cp = new CompilerParameters();
            cp.OutputAssembly = Util.GetTableFieldsPath + name;// Util.OTPath + "\\Module\\TableFields\\" + name;
            cp.ReferencedAssemblies.Add("System.dll");
            cp.ReferencedAssemblies.Add("System.Data.dll");
            cp.ReferencedAssemblies.Add("System.Xml.dll");
            cp.ReferencedAssemblies.Add("mscorlib.dll");
            cp.ReferencedAssemblies.Add("System.Windows.Forms.dll");
            cp.ReferencedAssemblies.Add("RuleCode.Common.dll");
            //cp.ReferencedAssemblies.Add("RuleCode.ObjectTree");

            cp.WarningLevel = 3;


            cp.CompilerOptions = "/target:module /optimize /moduleassemblyname:TableField.dll";
            cp.GenerateExecutable = false;
            cp.GenerateInMemory = false;

            TempFileCollection tfc = new TempFileCollection(Application.StartupPath, false);
            CompilerResults cr = new CompilerResults(tfc);
            CSharpCodeProvider csp = new CSharpCodeProvider();
            DirectoryInfo EDTDir = new DirectoryInfo(EDTPath);
            if (!EDTDir.Exists)
            {
                EDTDir.Create();
            }
            IndentedTextWriter tw = new IndentedTextWriter(new StreamWriter(EDTPath + name + ".cs", false), "    ");
            csp.GenerateCodeFromCompileUnit(CompileUnit, tw, new CodeGeneratorOptions());

            tw.Close();
            /*
            cr = csp.CompileAssemblyFromDom(cp, CompileUnit);

            if (cr.Errors.Count > 0)
            {
                foreach (CompilerError ce in cr.Errors)
                {
                    Console.WriteLine(ce.ErrorNumber + ": " + ce.ErrorText);
                }
                MessageBox.Show("Errors occoured", "Errors", MessageBoxButtons.OK, MessageBoxIcon.Error);

            } */

            #endregion


            //将编译 好的文件添加到项目中
            if (Util_DTE.IsAddToProjectAtCompiled)
                Util_DTE.AddGeneratedFileToProject(node, GenerateFileType.TableField);
        }


        private string typeNameMapping(BaseProperty _yang)
        {
            if (_yang is TableFieldStringProperty)
            {
                return "TString";
            }
            if (_yang is TableFieldIntProperty)
            {
                return "TInt";
            }
            if (_yang is TableFieldIntProperty)
            {
                return "TInt64";
            }
            if (_yang is TableFieldRealProperty)
            {
                return "TDouble";
            }
            if (_yang is TableFieldEnumProperty)
            {
                return "TEnum";
            }
            if (_yang is TableFieldContainerProperty)
            {
                return "TContainer";
            }
            if (_yang is TableFieldDateProperty)
            {
                return "TDate";
            }
            if (_yang is TableFieldTimeProperty)
            {
                return "TTime";
            }

            return "";
        }
    }
}